package com.yuzhe.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.yuzhe.model.Banner;
import com.yuzhe.model.Home;
import com.yuzhe.model.HomeType;
import com.yuzhe.utils.JSONResult;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;

import java.util.*;

import static com.yuzhe.common.Constant.BASE_SERVER_URL;

public class HomeController extends Controller {

    public void getHomeList() {
        String pageNumberStr = getPara("pageNumber");
        int pageNum = 0;
        if (!StringUtils.isBlank(pageNumberStr)) {
            pageNum = Integer.parseInt(pageNumberStr);
        }

        Page<Home> paginate = Home.dao.paginate(pageNum, 10, "select home.*,news.title as newsTitle,home_type.typename", "from home left join news on home.news_id = news.id join home_type on home.home_type_id = home_type.id");
        renderJson(JSONResult.ok(paginate));
    }

    public void getFrontHomeList() {
        Map<String, Object> homeMap = new HashMap<String, Object>();

        //获取首页banner列表
        String bannerSql = "select * from banner order by sort desc limit 3";
        List<Banner> banners = Banner.dao.find(bannerSql);
        homeMap.put("bannerList", banners);

        String homeTypeSql = "select * from home_type order by sort desc limit 6";
        List<HomeType> homeTypes = HomeType.dao.find(homeTypeSql);
        List homeTypeList = new ArrayList();

        for (int i = 0; i < homeTypes.size(); i++) {
            Map<String,Object> typeMap = new HashMap<String, Object>();
            typeMap.put("typename",homeTypes.get(i).getTypename());
            String id = homeTypes.get(i).getId();
            String homeContentList = "select * from home where home_type_id = ? order by sort desc limit 11";
            List<Home> homes = Home.dao.find(homeContentList, id);
            for (int j=0;j<homes.size();j++){
                Home home = homes.get(j);
                String imageUrl = home.getImageUrl();
                home.setImageUrl(BASE_SERVER_URL + imageUrl);
            }
            typeMap.put("contentlist",homes);
            homeTypeList.add(typeMap);
        }
        homeMap.put("homeTypeList", homeTypeList);
        renderJson(JSONResult.ok(homeMap));
    }

    //根据 id来查询后台首页数据
    public void getHomeListByTypeId() {
        String pageNumberStr = getPara("pageNumber");
        String typeId = getPara("typeId");
        int pageNum = 0;

        if (StringUtils.isBlank(pageNumberStr)){
            pageNum = 1;
        }

        if (!StringUtils.isBlank(pageNumberStr)) {
            pageNum = Integer.parseInt(pageNumberStr);
        }
        Page<Home> paginate;
        if (StringUtils.isBlank(typeId)){
             paginate = Home.dao.paginate(pageNum, 10, "select home.*,news.title as newsTitle,home_type.typename", "from home left join news on home.news_id = news.id join home_type on home.home_type_id = home_type.id");
        } else {
           paginate = Home.dao.paginate(pageNum, 10, "select home.*,news.title as newsTitle,home_type.typename", "from home left join news on home.news_id = news.id join home_type on home.home_type_id = home_type.id where home_type_id = ?",typeId);
        }
        renderJson(JSONResult.ok(paginate));
    }

    //根据 id来查询后台首页数据
    public void getHomeListByTitle() {
        String pageNumberStr = getPara("pageNumber");
        String title = getPara("title");
        int pageNum = 0;

        if (StringUtils.isBlank(pageNumberStr)){
            pageNum = 1;
        }

        if (!StringUtils.isBlank(pageNumberStr)) {
            pageNum = Integer.parseInt(pageNumberStr);
        }
        Page<Home> paginate;
        if (StringUtils.isBlank(title)){
            paginate = Home.dao.paginate(pageNum, 10, "select home.*,news.title as newsTitle,home_type.typename", "from home left join news on home.news_id = news.id join home_type on home.home_type_id = home_type.id");
        } else {
            paginate = Home.dao.paginate(pageNum, 10, "select home.*,news.title as newsTitle,home_type.typename", "from home left join news on home.news_id = news.id join home_type on home.home_type_id = home_type.id where home.title like ?","%"+title+"%");
        }
        renderJson(JSONResult.ok(paginate));
    }

    //根据 id来查询后台首页数据
    public void getHomeListByTitleAndTypeId() {
        String pageNumberStr = getPara("pageNumber");
        String title = getPara("title");
        String typeId = getPara("typeId");
        int pageNum = 0;

        if (StringUtils.isBlank(pageNumberStr)){
            pageNum = 1;
        }

        if (!StringUtils.isBlank(pageNumberStr)) {
            pageNum = Integer.parseInt(pageNumberStr);
        }

        if (StringUtils.isBlank(title) && StringUtils.isBlank(typeId)){
            renderJson(JSONResult.errorMsg("标题和类型不能为空"));
            return;
        }


        Page<Home> paginate = Home.dao.paginate(pageNum, 10, "select home.*,news.title as newsTitle,home_type.typename", "from home left join news on home.news_id = news.id join home_type on home.home_type_id = home_type.id where home_type_id = ? and home.title like ?",typeId,"%"+title+"%");

        renderJson(JSONResult.ok(paginate));
    }


    public void saveHome() {
        String title = getPara("title");
        String desc = getPara("desc");
        String homeTypeId = getPara("type");
        String imagePath = getPara("imagePath");
        String newsId = getPara("newsId");
        String sort = getPara("sort");


        if (StringUtils.isBlank(newsId)) {
            renderJson(JSONResult.errorMsg("资讯文章不能为空"));
            return;
        }

        if (StringUtils.isBlank(homeTypeId)) {
            renderJson(JSONResult.errorMsg("首页分类不能为空"));
            return;
        }

        if (StringUtils.isBlank(sort)) {
            sort = "50";
        }
        String id = Sid.next();
        boolean save = Home.dao.setId(id).setTitle(title)
                .setNewsId(newsId)
                .setDesc(desc)
                .setHomeTypeId(homeTypeId)
                .setImageUrl(imagePath)
                .setSort(Integer.valueOf(sort))
                .setCreateTime(new Date()).save();
        if (save) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("保存失败"));
        }

    }

    //更新home
    public void updateHome() {

        String title = getPara("title");
        String id = getPara("id");
        String imagePath = getPara("imagePath");
        String newsId = getPara("newsId");
        String desc = getPara("desc");
        String homeTypeId = getPara("homeTypeId");
        String sort = getPara("sort");

        if (StringUtils.isBlank(id)) {
            renderJson(JSONResult.errorMsg("id不能为空"));
            return;
        }


        if (StringUtils.isBlank(imagePath)) {
            renderJson(JSONResult.errorMsg("轮播图片不能为空"));
            return;
        }

        if (StringUtils.isBlank(sort)) {
            sort = "50";
        }

        boolean save = Home.dao.setId(id).setTitle(title)
                .setNewsId(newsId)
                .setDesc(desc)
                .setHomeTypeId(homeTypeId)
                .setSort(Integer.valueOf(sort))
                .setImageUrl(imagePath).update();
        if (save) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("保存失败"));
        }

    }

    //删除轮播图
    public void deleteHome() {
        String ids = getPara("ids");
        if (StringUtils.isBlank(ids)) {
            renderJson(JSONResult.errorMsg("轮播图id不能为空"));
            return;
        }

        String sql = "delete from home where id in (" + ids + ")";
        int update = Db.update(sql);
        if (update > 0) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("批量删除失败"));
        }
    }
}
